package project.tests;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import java.awt.GridLayout;
import javax.swing.JTextField;
import javax.swing.JButton;

import project.map.OccupancyGridMap;
import project.map.Position;
import project.robot.*;
import project.robot.navigation.ChessGridNavigator;
import project.simulador.*;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import lejos.robotics.navigation.Navigator;


public class NavegadorTest extends JFrame {

	private JPanel contentPane;
	private JTextField txtLinea;
	private JTextField txtSuelo;
	private JTextField txtVelocidad;
	private JTextField txtKp;
	private JTextField txtAnchoRobot;
	private JTextField txtDiametroRuedas;
	private JTextField txtEsperarLinea;
	private JTextField txtUmbralLinea;

	private ChessGridNavigator navegador;
	private Robot robot;
	private JTextField txtRotar;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					NavegadorTest frame = new NavegadorTest();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public NavegadorTest() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 507, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(new BorderLayout(0, 0));

		JPanel panel = new JPanel();
		contentPane.add(panel, BorderLayout.WEST);
		panel.setLayout(new GridLayout(0, 2, 0, 0));

		JLabel lblNewLabel = new JLabel("Linea:");
		panel.add(lblNewLabel);

		txtLinea = new JTextField();
		panel.add(txtLinea);
		txtLinea.setColumns(10);

		JLabel lblNewLabel_1 = new JLabel("Suelo:");
		panel.add(lblNewLabel_1);

		txtSuelo = new JTextField();
		panel.add(txtSuelo);
		txtSuelo.setColumns(10);

		JLabel lblNewLabel_2 = new JLabel("Velocidad:");
		panel.add(lblNewLabel_2);

		txtVelocidad = new JTextField();
		panel.add(txtVelocidad);
		txtVelocidad.setColumns(10);

		JLabel lblNewLabel_3 = new JLabel("Kp:");
		panel.add(lblNewLabel_3);

		txtKp = new JTextField();
		panel.add(txtKp);
		txtKp.setColumns(10);

		JLabel lblNewLabel_4 = new JLabel("Ancho Robot:");
		panel.add(lblNewLabel_4);

		txtAnchoRobot = new JTextField();
		panel.add(txtAnchoRobot);
		txtAnchoRobot.setColumns(10);

		JLabel lblNewLabel_5 = new JLabel("Diametro Ruedas:");
		panel.add(lblNewLabel_5);

		txtDiametroRuedas = new JTextField();
		panel.add(txtDiametroRuedas);
		txtDiametroRuedas.setColumns(10);

		JLabel lblNewLabel_6 = new JLabel("Esperar Linea:");
		panel.add(lblNewLabel_6);

		txtEsperarLinea = new JTextField();
		panel.add(txtEsperarLinea);
		txtEsperarLinea.setColumns(10);

		JLabel lblNewLabel_7 = new JLabel("Umbral Linea:");
		panel.add(lblNewLabel_7);

		txtUmbralLinea = new JTextField();
		panel.add(txtUmbralLinea);
		txtUmbralLinea.setColumns(10);

		JPanel panel_1 = new JPanel();
		contentPane.add(panel_1, BorderLayout.EAST);
		panel_1.setLayout(new GridLayout(9, 1, 0, 0));

		JPanel panel_2 = new JPanel();
		panel_1.add(panel_2);

		JButton btnArriba = new JButton("N");
		btnArriba.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				robot.orientToNorth();
				robot.goForward(20, 20);
			}
		});
		panel_2.add(btnArriba);

		JPanel panel_3 = new JPanel();
		panel_1.add(panel_3);

		JButton btnIzquierda = new JButton("W");
		btnIzquierda.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				robot.orientToWest();
				robot.goForward(20, 20);
			}
		});
		
		JButton btnAvanzar = new JButton("Avanzar");
		btnAvanzar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				robot.goForward(20, Integer.parseInt(txtVelocidad.getText()));
			}
		});
		panel_3.add(btnAvanzar);
		panel_3.add(btnIzquierda);

		JButton btnStop = new JButton("[S]");
		btnStop.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				robot.orientToSouth();
				robot.goForward(20, 20);
			}
		});
		panel_3.add(btnStop);

		JButton btnDerecha = new JButton("E");
		btnDerecha.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				robot.orientToEast();
				robot.goForward(20, 20);
			}
		});
		panel_3.add(btnDerecha);

		JPanel panel_5 = new JPanel();
		panel_1.add(panel_5);

		JButton btnNewButton = new JButton("Izq");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				robot.turn(90);
			}
		});
		panel_5.add(btnNewButton);

		JButton btnNewButton_1 = new JButton("Der");
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				robot.turn(-90);
				
			}
		});
		panel_5.add(btnNewButton_1);
		
		JButton btnStartDetection = new JButton("Start Detection");
		btnStartDetection.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				robot.startDetection();
			}
		});
		panel_5.add(btnStartDetection);
		
		JButton btnStopDetection = new JButton("Stop Detection");
		btnStopDetection.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				robot.stopDetection();
			}
		});
		panel_5.add(btnStopDetection);
		
		JPanel panel_6 = new JPanel();
		panel_1.add(panel_6);
		
		txtRotar = new JTextField();
		panel_6.add(txtRotar);
		txtRotar.setColumns(10);
		
		JButton btnRotar = new JButton("Rotar");
		btnRotar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				int angle = Integer.parseInt(txtRotar.getText());
				robot.turn(angle);
			}
		});
		panel_6.add(btnRotar);
		
		JButton btnUpdatefrontier = new JButton("UpdateFrontier");
		btnUpdatefrontier.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				robot.updateFrontier();
			}
		});
		panel_6.add(btnUpdatefrontier);
		
		JButton btnCorregir = new JButton("Corregir");
		btnCorregir.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				//navegador.correctionRutine();
			}
		});
		panel_6.add(btnCorregir);
		
		JPanel panel_7 = new JPanel();
		panel_1.add(panel_7);
		
		JButton btnStartMapping = new JButton("Start Mapping");
		btnStartMapping.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				robot.startMapping(5);
			}
		});
		panel_7.add(btnStartMapping);
		
		JButton btnVerMapa = new JButton("VerMapa");
		btnVerMapa.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				robot.getMapa().paint();
			}
		});
		panel_7.add(btnVerMapa);
		
		JButton btnInproximo = new JButton("inProximo");
		btnInproximo.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				
				
			}
		});
		panel_7.add(btnInproximo);
		
		JButton btnadelante = new JButton("10Adelante");
		btnadelante.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				
			}
		});
		panel_7.add(btnadelante);
		
		JButton btnatras = new JButton("10Atras");
		btnatras.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
			}
		});
		panel_7.add(btnatras);

		JPanel panel_4 = new JPanel();
		contentPane.add(panel_4, BorderLayout.CENTER);

		JButton btnReset = new JButton("Reset");
		btnReset.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				btnReset();
			}
		});
		panel_4.add(btnReset);

		inicializar();
	}

	private void inicializar()
	{
		//setear los valor por defecto
		txtLinea.setText("550");
		txtSuelo.setText("300");
		txtVelocidad.setText("80");
		txtKp.setText("0.1");
		txtAnchoRobot.setText("10.5");
		txtDiametroRuedas.setText("4.32");
		txtEsperarLinea.setText("4500");
		txtUmbralLinea.setText("400");
	}

	private void btnReset()
	{
		navegador = new ChessGridNavigator();

		//navegador.setLinea(Float.parseFloat(txtLinea.getText()));
		//navegador.setSuelo(Float.parseFloat(txtSuelo.getText()));
		//navegador.setKp(Float.parseFloat(txtKp.getText()));
		//navegador.setAnchoRobot(Float.parseFloat(txtAnchoRobot.getText()));
		//navegador.setDiametroRuedas(Float.parseFloat(txtDiametroRuedas.getText()));
			
		OccupancyGridMap map = new OccupancyGridMap(8,9);
		
		robot = new Robot(80,1000);
		robot.setMapa(map);
		robot.setPosActual(new Position(6,1,1));
		robot.setNavegacion(navegador);
	}
}
